  import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'logic.dart';

class DecryptPage extends StatefulWidget {
  @override
  _DecryptPageState createState() => _DecryptPageState();
}

class _DecryptPageState extends State<DecryptPage> {

  @override
  Widget build(BuildContext context) {
    final logic = Get.find<DecryptLogic>();
    return Scaffold( // 添加 Scaffold
        body:Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              // 明文输入框
              TextFormField(
                controller: logic.cipherTextController,
                decoration: InputDecoration(
                  isCollapsed: true, // 关键属性[1](@ref)
                  contentPadding: const EdgeInsets.symmetric(
                      vertical: 10, // 垂直间距控制高度
                      horizontal: 10),
                  labelText: '密文数据',
                  hintText: '请输入密文',
                  border: OutlineInputBorder(),
                ),
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return '密文不能为空';
                  }
                  return null;
                },
              ),
              const SizedBox(height: 20),
              // 密钥输入框
              TextFormField(
                controller: logic.KeyController,
                decoration: InputDecoration(
                  isCollapsed: true, // 关键属性[1](@ref)
                  contentPadding: const EdgeInsets.symmetric(
                      vertical: 10, // 垂直间距控制高度
                      horizontal: 10),
                  labelText: '密钥',
                  hintText: '请输入解密密钥',
                  border: OutlineInputBorder(),
                ),
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return '密钥不能为空';
                  }
                  return null;
                },
              ),
              const SizedBox(height: 20),

              // 初始化向量
              TextFormField(
                controller: logic.ivText,
                decoration: InputDecoration(
                  isCollapsed: true, // 关键属性[1](@ref)
                  contentPadding: const EdgeInsets.symmetric(
                      vertical: 10, // 垂直间距控制高度
                      horizontal: 10),
                  labelText: '初始化向量IV',
                  hintText: '请输入初始化向量IV',
                  border: OutlineInputBorder(),
                ),
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return '初始化向量IV不能为空';
                  }
                  return null;
                },
              ),
              const SizedBox(height: 20),

              Row(
                children: [
                  Expanded(
                    child: ElevatedButton.icon(
                      icon: const Icon(Icons.vpn_key), // 修正图标名称
                      label: const Text('AES解密'),
                      style: ElevatedButton.styleFrom(
                        padding: const EdgeInsets.symmetric(vertical: 16),
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(8),
                        ),
                      ),
                      onPressed: logic.decryptAes,
                    ),
                  ),
                  const SizedBox(width: 10),
                  Expanded(
                    child: ElevatedButton.icon(
                      icon: const Icon(Icons.vpn_key),
                      label: const Text('私钥解密'),
                      style: ElevatedButton.styleFrom(
                        padding: const EdgeInsets.symmetric(vertical: 16),
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(8),
                        ),
                      ),
                      onPressed: logic.decryptEcc,
                    ),
                  ),
                ],
              ),
              const SizedBox(height: 20),
              Expanded(
                child: Container(
                  padding: const EdgeInsets.all(16),
                  decoration: BoxDecoration(
                    border: Border.all(color: Colors.blue.shade200),
                    borderRadius: BorderRadius.circular(12),
                  ),
                  child: Obx(() => SingleChildScrollView(
                    child: SelectableText(
                      logic.result.value,
                      style: const TextStyle(
                        fontFamily: 'RobotoMono',
                        fontSize: 14,
                        height: 1.6,
                      ),
                    ),
                  )),
                ),
              ),
            ],
          ),
        )
    );
  }
}
